//
//  GRPayBillView.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/7/9.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRPayBillView.h"
@interface GRPayBillView ()

@property (weak, nonatomic) IBOutlet UIButton *openInvoiceBt;
@property (weak, nonatomic) IBOutlet UIButton *personalBt;

@property (weak, nonatomic) IBOutlet UIButton *companyBt;

@property (weak, nonatomic) IBOutlet UITextField *personSnTF;

@property (weak, nonatomic) IBOutlet UITextField *phoneNumTF;

@property (weak, nonatomic) IBOutlet UITextField *invoiceNameTF;

@property (weak, nonatomic) IBOutlet UIButton *eleInvoiceBt;
@property (weak, nonatomic) IBOutlet UIButton *paperInvoiceBt;
@property (weak, nonatomic) IBOutlet UIButton *goodDetailBt;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomViewTop;


@end
@implementation GRPayBillView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

+ (instancetype)sharedView
{
    static GRPayBillView *payBill;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
       payBill = [GRPayBillView xx_instantiateFromNib];
    });
    return payBill;
}
+(void)ShowInView:(UIView*)view Delegate:(id<GRPayBillViewDelegate>)delegate{
    [GRPayBillView sharedView].delegate = delegate;
    GRInvoiceModel * invoiceMdoel =   [[GRInvoiceModel alloc]init];
    invoiceMdoel.invoice = YES;
    [[GRPayBillView sharedView] updateUIWith:invoiceMdoel ];
    [[GRPayBillView sharedView] showInView:view];
    
}

-(void)updateUIWith:(GRInvoiceModel *)model{
    self.invoiceModel = model;
    self.openInvoiceBt.selected = model.invoice;
    self.goodDetailBt.selected = model.invoice;
    self.personalBt.userInteractionEnabled = model.invoice;
    self.companyBt.userInteractionEnabled = model.invoice;
    self.eleInvoiceBt.userInteractionEnabled = model.invoice;
    self.paperInvoiceBt.userInteractionEnabled = model.invoice;
    if (!model.invoice) {
        self.personalBt.selected = model.invoice;
        self.companyBt.selected = model.invoice;
        self.eleInvoiceBt.selected = model.invoice;
        self.paperInvoiceBt.selected = model.invoice;
    }else{
         self.personalBt.selected = model.invoice;
        self.eleInvoiceBt.selected = model.invoice;
    }
    NSString * imageName = model.invoice?@"goodDetail_04":@"goodDetail_05";
    [self.personalBt setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [self.companyBt setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [self.eleInvoiceBt setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [self.paperInvoiceBt setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    UIColor *normalTextColor = model.invoice? HEXCOLOR(0x222222):HEXCOLOR(0x999999);
    [self.personalBt setTitleColor:normalTextColor forState:UIControlStateNormal];
    [self.companyBt setTitleColor:normalTextColor forState:UIControlStateNormal];
    [self.eleInvoiceBt setTitleColor:normalTextColor forState:UIControlStateNormal];
    [self.paperInvoiceBt setTitleColor:normalTextColor forState:UIControlStateNormal];
    [self.goodDetailBt setTitleColor:normalTextColor forState:UIControlStateNormal];
}


- (void)showInView:(UIView *)view
{
    self.frame = view.bounds;
    [view addSubview:self];
}

- (IBAction)closeAction:(id)sender {

    [self removeFromSuperview];

    
}

+(void)dismiss{
    [[GRPayBillView sharedView]closeAction:nil];
}

- (IBAction)openInvoiceAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    self.invoiceModel.invoice = sender.selected;
    [self updateUIWith:self.invoiceModel];
}


- (IBAction)invoiceTypeAction:(UIButton *)sender {
    self.personalBt.selected = NO;
    self.companyBt.selected = NO;
    sender.selected = YES;
    self.invoiceModel.invoiceType = sender.tag -100;
    [self changeBottmeView];
}

-(void)changeBottmeView{
    WEAKSELF
    [UIView animateWithDuration:0.25 animations:^{
        if (weakSelf.invoiceModel.invoiceType == 0) {
            weakSelf.bottomViewTop.constant = -89;
        }else{
           weakSelf.bottomViewTop.constant = 1;
        }
    }];
    
}

- (IBAction)invoiceFlagAction:(UIButton *)sender {
    self.eleInvoiceBt.selected = NO;
    self.paperInvoiceBt.selected = NO;
    sender.selected = YES;
    self.invoiceModel.invoiceFlg = sender.tag -1000;
}

- (IBAction)confirmAction:(id)sender {
    self.invoiceModel.personSn = self.personSnTF.text;
    self.invoiceModel.invoiceMobile = self.phoneNumTF.text;
    self.invoiceModel.invoiceName = self.invoiceNameTF.text;
        if (_delegate && [_delegate respondsToSelector:@selector(customerNeedInvoiceType:)]) {
            [_delegate customerNeedInvoiceType:self.invoiceModel];
        }
    [self removeFromSuperview];
    
}


@end
